﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace QLTV_GiaoDien
{
    public partial class frm_NguoiDung : DevExpress.XtraEditors.XtraForm
    {
        public frm_NguoiDung()
        {
            InitializeComponent();
        }
        private int _Flag;

        public int Flag
        {
            get { return _Flag; }
            set { _Flag = value; }
        }
        private int _MaNV;

        public int MaNV
        {
            get { return _MaNV; }
            set { _MaNV = value; }
        }
        private string _Username;

        public string Username
        {
            get { return _Username; }
            set { _Username = value; }
        }
        private string _Password;

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }
        private string _HoTen;

        public string HoTen
        {
            get { return _HoTen; }
            set { _HoTen = value; }
        }
        private string _NgaySinh;

        public string NgaySinh
        {
            get { return _NgaySinh; }
            set { _NgaySinh = value; }
        }
        private string _GioiTinh;

        public string GioiTinh
        {
            get { return _GioiTinh; }
            set { _GioiTinh = value; }
        }
        private int _SoCMT;

        public int SoCMT
        {
            get { return _SoCMT; }
            set { _SoCMT = value; }
        }
        private string _DiaChi;

        public string DiaChi
        {
            get { return _DiaChi; }
            set { _DiaChi = value; }
        }
        private string _TenNhom;

        public string TenNhom
        {
            get { return _TenNhom; }
            set { _TenNhom = value; }
        }
        private string _ChucDanh;

        public string ChucDanh
        {
            get { return _ChucDanh; }
            set { _ChucDanh = value; }
        }
        private string _DienThoai;

        public string DienThoai
        {
            get { return _DienThoai; }
            set { _DienThoai = value; }
        }
        private string _Email;

        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void enableTextFalse(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Enabled = false;
            }
            foreach (Control i in ctr.Controls)
                enableTextFalse(i);
        }
        
        public void LoadForm()
        {
            txt_MaNV.Text = MaNV.ToString();
            txt_HoTen.Text = HoTen;
            dateTime_NgaySinh.Text = NgaySinh;
            cob_GioiTinh.Text = GioiTinh;
            txt_DiaChi.Text = DiaChi;
            msk_CMT.Text = SoCMT.ToString();
            txt_ChucDanh.Text = ChucDanh;
            cob_Nhom.Text = TenNhom;
            msk_DienThoai.Text = DienThoai.ToString();
            txt_Email.Text = Email;
            txt_TenDn.Text = Username;
            txt_MatKhau.Text = Password;
            //

        }
        public void GetData()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Mới Người Dùng";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
            }
            if (Flag == 1)
            {
                LoadForm();
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                this.Text = "Sửa Người Dùng";
            }
            if (Flag == 2)
            {
                LoadForm();
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                enableTextFalse(this);
                this.Text = "Xem Người Dùng";
                
            }
        }
        NguoiDung_BLl nguoiDung_bll = new NguoiDung_BLl();
        private void frm_NguoiDung_Load(object sender, EventArgs e)
        {
            
            cob_MaNhom.Hide();
            string sql = "select * from tbl_NhomQuyen";
            DataTable dt = new DataTable();
            dt = nguoiDung_bll.TaoBang(sql);
            cob_Nhom.DataSource = dt;
            cob_Nhom.DisplayMember = "TenNhom";
            GetData();
            
        }
        public delegate void GetInt(int kt);
        public GetInt MyGetData;
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NguoiDung_Public nv = new NguoiDung_Public();
            if (Flag == 0)
            {
                //them
                nv.Username = txt_TenDn.Text;
                nv.Password = txt_MatKhau.Text;
                nv.HoTen = txt_HoTen.Text;
                nv.NgaySinh=Convert.ToDateTime(dateTime_NgaySinh.Text);
                nv.GioiTinh = cob_GioiTinh.Text;
                nv.SoCMT = int.Parse(msk_CMT.Text);
                nv.DiaChi = txt_DiaChi.Text;
                nv.MaNhom = int.Parse(cob_MaNhom.Text);
                nv.ChucDanh = txt_ChucDanh.Text;
                nv.DienThoai = msk_DienThoai.Text;
                nv.Email = txt_Email.Text;
                nguoiDung_bll.insert_NguoiDung(nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm  Người dùng hệ thống có tài khoản: '" +txt_TenDn.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (Flag == 1)
            {
                //sua
                nv.MaNV = int.Parse(txt_MaNV.Text);
                nv.Username = txt_TenDn.Text;
                nv.Password = txt_MatKhau.Text;
                nv.HoTen = txt_HoTen.Text;
                nv.NgaySinh = Convert.ToDateTime(dateTime_NgaySinh.Text);
                nv.GioiTinh = cob_GioiTinh.Text;
                nv.SoCMT = int.Parse(msk_CMT.Text);
                nv.DiaChi = txt_DiaChi.Text;
                nv.MaNhom = int.Parse(cob_MaNhom.Text);
                nv.ChucDanh = txt_ChucDanh.Text;
                nv.DienThoai = msk_DienThoai.Text;
                nv.Email = txt_Email.Text;
                nguoiDung_bll.update_NguoiDung(nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv,nv);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Người dùng hệ thống: '" +txt_TenDn.Text+ "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (Flag == 2)
            {
                //xem
                nv.MaNV = int.Parse(txt_MaNV.Text);
                nv.Username = txt_TenDn.Text;
                nv.Password = txt_MatKhau.Text;
                nv.HoTen = txt_HoTen.Text;
                nv.NgaySinh = Convert.ToDateTime(dateTime_NgaySinh.Text);
                nv.GioiTinh = cob_GioiTinh.Text;
                nv.SoCMT = int.Parse(msk_CMT.Text);
                nv.DiaChi = txt_DiaChi.Text;
                nv.MaNhom = int.Parse(cob_MaNhom.Text);
                nv.ChucDanh = txt_ChucDanh.Text;
                nv.DienThoai = msk_DienThoai.Text;
                nv.Email = txt_Email.Text;
                nguoiDung_bll.update_NguoiDung(nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv, nv);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Người dùng hệ thống: '" + txt_TenDn.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            Huy_Text(this);
            MyGetData(1);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            
        }

        private void cob_Nhom_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_NhomQuyen";
            dt = nguoiDung_bll.TaoBang(sql);
            cob_MaNhom.DataSource = dt;
            cob_MaNhom.DisplayMember = "MaNhom";
            cob_Nhom.DataSource = dt;
            cob_Nhom.DisplayMember = "TenNhom";
        }


        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Huy_Text(this);
            //enableTextFalse(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled =false;
            bbi_Huy.Enabled = false;
            Flag = 4;
        }

        private void cob_Nhom_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_NhomQuyen where TenNhom=N'" + cob_Nhom.Text + "'";
            dt = nguoiDung_bll.TaoBang(sql);
            cob_MaNhom.DataSource = dt;
            cob_MaNhom.DisplayMember = "MaNhom";
        }

        private void txt_MaNV_Leave(object sender, EventArgs e)
        {
            if (txt_MaNV.Text == "")
                txt_MaNV.Focus();
        }

        private void txt_MaNV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            txt_MaNV.Enabled = false;
            txt_MaNV.Text = "Mã tự sinh";
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
        }
    }
}